package com.yealove.lunchparty.demo.mybatis;

import com.yealove.lunchparty.demo.bean.Tester;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * User: yealove
 * Date: 8/17/14
 * Time: 5:28 PM
 * Created with IntelliJ IDEA.
 */
public class HelloMybatis {
    private static SqlSessionFactory sqlSessionFactory;
    private static Logger logger = LoggerFactory.getLogger(HelloMybatis.class);

    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<Tester> testers = sqlSession.selectList("getUsers");
        for (Tester tester : testers) {
            logger.info("user[id={} name={}]", tester.getId(), tester.getName());
        }
    }
}
